package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendPunchDeptDAO;
import org.jsola.hr.dto.AttendPunchDeptAddDTO;
import org.jsola.hr.dto.AttendPunchDeptUpdateDTO;
import org.jsola.hr.entity.AttendPunchDeptDO;
import org.jsola.hr.query.AttendPunchDeptQuery;
import org.jsola.hr.service.IAttendPunchDeptService;
import org.jsola.hr.vo.AttendPunchDeptListVO;
import org.jsola.hr.vo.AttendPunchDeptVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendPunchDeptServiceImpl")
public class AttendPunchDeptServiceImpl implements IAttendPunchDeptService {

    @Autowired
    private IAttendPunchDeptDAO attendPunchDeptDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendPunchDeptVO save(AttendPunchDeptAddDTO attendPunchDeptAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendPunchDeptDO attendPunchDeptDO = attendPunchDeptAddDTO.to(AttendPunchDeptDO.class);
        // 保存
        attendPunchDeptDO = save(attendPunchDeptDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendPunchDeptDO.to(AttendPunchDeptVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendPunchDeptUpdateDTO attendPunchDeptUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendPunchDeptDO attendPunchDeptDO = attendPunchDeptUpdateDTO.to(AttendPunchDeptDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendPunchDeptDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendPunchDeptIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendPunchDeptIds);
    }


    @Override
    public AttendPunchDeptVO selectById(String attendPunchDeptId, String siteId) {
        AttendPunchDeptDO attendPunchDeptDO = selectDOById(attendPunchDeptId, siteId);
        if (attendPunchDeptDO == null) {
            return null;
        }
        return attendPunchDeptDO.to(AttendPunchDeptVO.class);
    }

    @Override
    public List<AttendPunchDeptListVO> select(AttendPunchDeptQuery attendPunchDeptQuery, String siteId) {
        List<AttendPunchDeptDO> attendPunchDeptDOList = selectDO(attendPunchDeptQuery, siteId);
        if(CollectionUtils.isEmpty(attendPunchDeptDOList)) {
            return attendPunchDeptDOList == null ? null : new ArrayList<>();
        }
        return attendPunchDeptDOList.stream()
                .map(attendPunchDeptDO -> attendPunchDeptDO.to(AttendPunchDeptListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendPunchDeptQuery attendPunchDeptQuery, String siteId) {
        Example example = buildExample(attendPunchDeptQuery,siteId);
        return attendPunchDeptDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendPunchDeptListVO> selectPage(AttendPunchDeptQuery attendPunchDeptQuery, String siteId) {
        Example example = buildExample(attendPunchDeptQuery,siteId);
        Page<AttendPunchDeptDO> page = attendPunchDeptDAO.selectPageByExample(example,
                attendPunchDeptQuery.getPageNo(),
                attendPunchDeptQuery.getPageSize());

        return page.to(AttendPunchDeptListVO.class);
    }

    @Override
    public AttendPunchDeptDO selectDOById(String attendPunchDeptId, String siteId) {
        return listById(attendPunchDeptId, siteId);
    }

    @Override
    public List<AttendPunchDeptDO> selectDO(AttendPunchDeptQuery attendPunchDeptQuery, String siteId) {
        Example example = buildExample(attendPunchDeptQuery,siteId);
        return attendPunchDeptDAO.selectByExample(example);
    }

    @Override
    public int deleteByAttendPlanId(String attendPlanId, String siteId) {
        AttendPunchDeptQuery attendPunchDeptQuery = new AttendPunchDeptQuery();
        attendPunchDeptQuery.setAttendPlanId(attendPlanId);
        Example example = buildExample(attendPunchDeptQuery,siteId);
        return attendPunchDeptDAO.deleteByExample(example);
    }

    @Override
    public int logicDelByAttendPlanId(String attendPlanId, TokenUser tokenUser) {

        // 查询
        AttendPunchDeptQuery attendPunchDeptQuery = new AttendPunchDeptQuery();
        attendPunchDeptQuery.setAttendPlanId(attendPlanId);
        List<AttendPunchDeptDO> dataList = this.selectDO(attendPunchDeptQuery, tokenUser.getSiteId());
        if (dataList.size() <= 0) {
            return 0;
        }

        for (AttendPunchDeptDO attendPunchDept : dataList) {

            AttendPunchDeptDO attendPunchDeptDO = new AttendPunchDeptDO();

            attendPunchDeptDO.setId(attendPunchDept.getId());
            attendPunchDeptDO.setValid(false);
            attendPunchDeptDO.setModifiedUserId(tokenUser.getUserId());
            attendPunchDeptDO.setGmtModified(new Date());

            attendPunchDeptDAO.updateByPrimaryKeySelective(attendPunchDeptDO);
        }

        return 0;
    }

    /**
     * 根据查询参数，构建example

     * @param attendPunchDeptQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendPunchDeptQuery attendPunchDeptQuery, String siteId) {
        Example example = new Example(AttendPunchDeptDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendPunchDeptQuery.getDeptIds() != null && attendPunchDeptQuery.getDeptIds().size() > 0) {
            example.and().andIn("deptId", attendPunchDeptQuery.getDeptIds());
        }
        if (attendPunchDeptQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendPunchDeptQuery.getCompanyId() + "%");
        }
        if (attendPunchDeptQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendPunchDeptQuery.getCompanyName() + "%");
        }
        if (attendPunchDeptQuery.getDeptId() != null) {
            example.and().andLike("deptId", "%" + attendPunchDeptQuery.getDeptId() + "%");
        }
        if (attendPunchDeptQuery.getDeptName() != null) {
            example.and().andLike("deptName", "%" + attendPunchDeptQuery.getDeptName() + "%");
        }
        if (attendPunchDeptQuery.getAttendPlanId() != null) {
            example.and().andLike("attendPlanId", "%" + attendPunchDeptQuery.getAttendPlanId() + "%");
        }
        if (attendPunchDeptQuery.getAttendPlanIdNotEq() != null) {
        	example.and().andNotEqualTo("attendPlanId", attendPunchDeptQuery.getAttendPlanIdNotEq());
        }
        if (attendPunchDeptQuery.getGmtCreateStart() != null &&
                attendPunchDeptQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendPunchDeptQuery.getGmtCreateStart(),
                    attendPunchDeptQuery.getGmtCreateEnd());
        }
        if (attendPunchDeptQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendPunchDeptQuery.getCreateUserId() + "%");
        }
        if (attendPunchDeptQuery.getGmtModifiedStart() != null &&
                attendPunchDeptQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendPunchDeptQuery.getGmtModifiedStart(),
                    attendPunchDeptQuery.getGmtModifiedEnd());
        }
        if (attendPunchDeptQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendPunchDeptQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendPunchDeptQuery.getOrders());
        return example;
    }
}




